package juego;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import titiritero.Gameloop;
import titiritero.SuperficieDeDibujo;
import titiritero.SuperficiePanel;
import titiritero.vista.MouseClickController;

public class VentanaPrincipalFlightControl {

	private JFrame frame;
	private Gameloop gameLoop;
	private JPanel panel;
	private boolean iniciado = false;
	private MouseClickController mouseClickJuego;
	
	/**
	 * Create the application.
	 */
	public VentanaPrincipalFlightControl() {
		
		initialize();

	}

	/**
	 * Initialize the contents of the frame.
	 * @throws IOException 
	 */
	private void initialize(){
		frame = new JFrame();
		frame.setForeground(new Color(0, 0, 0));
		frame.setBounds(100, 100, 620, 518);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setTitle("Flight Control");
		frame.setResizable(false);
		
		JButton btnIniciar = this.addBotonIniciar();
		
		JButton btnReiniciar = this.addBotonReiniciar();
		
		JButton btnDetener = this.addBotonDetener();
		
		panel = this.addSuperficiePanel();
		
		this.gameLoop = new Gameloop(true);
		
		mouseClickJuego = new MouseClickController(this.gameLoop);
		
		panel.addMouseListener(mouseClickJuego);
		
		this.gameLoop.setSuperficieDeDibujo((SuperficieDeDibujo) panel);
		
		this.inicializarModelo();
		
		this.addMouseListener(panel);
		
		this.addKeyListener();

		this.setComponentsFocus(btnIniciar, btnDetener, btnReiniciar);

	}

	private void inicializarModelo() {

	}

	private void setComponentsFocus(JButton btnIniciar, JButton btnDetener, JButton btnReiniciar) {
		frame.setFocusable(true);
		btnDetener.setFocusable(false);
		btnIniciar.setFocusable(false);
		btnReiniciar.setFocusable(false);
	}

	private void addKeyListener() {
		frame.addKeyListener(new KeyListener(
				) {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				System.out.println("Key pressed");
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				System.out.println("Ping");
			}  
			 	
		});
	}

	private void addMouseListener(JPanel panel) {
		panel.addMouseListener(new MouseAdapter() {
					
			@Override
			public void mouseClicked(MouseEvent arg0) {				
			}});
	}

	private JPanel addSuperficiePanel() {
		JPanel panel = new SuperficiePanel();
		panel.setBackground(new Color(0, 0, 0));
		panel.setBounds(2, 53, 610, 435);
		frame.getContentPane().add(panel);
		return panel;
	}

	private JButton addBotonDetener() {
		JButton btnDetener = new JButton("Detener");
		btnDetener.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (iniciado){
					gameLoop.detenerEjecucion();
				}
			}
		});
		btnDetener.setBounds(450, 16, 92, 25);
		frame.getContentPane().add(btnDetener);
		return btnDetener;
	}
	
	private JButton addBotonReiniciar() {
		JButton btnReiniciar = new JButton("Reiniciar");
		btnReiniciar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (iniciado){
					gameLoop.detenerEjecucion();
					
					panel.removeMouseListener(mouseClickJuego);
					
					gameLoop= gameLoop.getJuegoEnEjecucion().reiniciarJuego();
					
					mouseClickJuego = new MouseClickController(gameLoop);
					
					panel.addMouseListener(mouseClickJuego);
					
					gameLoop.setSuperficieDeDibujo((SuperficieDeDibujo) panel);
					
					gameLoop.iniciarEjecucion();
				}
			}
		});
		btnReiniciar.setBounds(275, 16, 92, 25);
		frame.getContentPane().add(btnReiniciar);
		return btnReiniciar;
	}
	

	private JButton addBotonIniciar() {
		JButton btnIniciar = new JButton("Iniciar");
		btnIniciar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(!iniciado){
					
					iniciado = true;
				}
				if(!gameLoop.estaEnEjecucion()){
					gameLoop.iniciarEjecucion();
				}
			}
		});
		btnIniciar.setBounds(100, 16, 77, 25);
		frame.getContentPane().add(btnIniciar);
		return btnIniciar;
	}
	
	public JFrame getFrame(){
		
		return this.frame;
	}
	
	public Gameloop getGameloop(){
		
		return this.gameLoop;
	}
}
